﻿using Common.Contract;
using Common.Contract.ProductHandbook;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service;
using Common.Service.DataTables;
using Common.Type;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Models.ViewModels.AdminProduct;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class AdminProductController : ControllerBase
    {

        #region General View
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult Product()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ProductData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookCategoryRequest request = new RetrieveProductHandbookCategoryRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProductPro"
                    },
                    Lang = CurrentLang,
                };
                var customerRecords = new RetrieveProductHandbookCategoryResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductHandbookCategory(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ProductHandbookCategoryRecord>() : customerRecords.Records;
                List<ProductHandbookProductRecord> listProduct = new List<ProductHandbookProductRecord>();
                resultCustomer.ForEach(item =>
                {
                    listProduct.AddRange(item.Product.Select(x => new ProductHandbookProductRecord
                    {
                        Category = item.Category,
                        ProductId = x.ProductId,
                        ProductName = x.ProductName,
                        HasInfo = x.HasInfo
                    }));
                });
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    sColumns = pageRequest.ColumnNames,
                    aaData = listProduct.Select(x => new object[]{
                                                             x.Category,
                                                             x.ProductName,
                                                             x.HasInfo,
                                                             x.ProductId
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        public ActionResult ProductProgram()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ProductProgramData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookCategoryRequest request = new RetrieveProductHandbookCategoryRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProductPro"
                    },
                    Lang = CurrentLang,
                };
                var customerRecords = new RetrieveProductHandbookCategoryResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductProgramHandbookCategory(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<ProductHandbookCategoryRecord>() : customerRecords.Records;
                List<ProductHandbookProductRecord> listProduct = new List<ProductHandbookProductRecord>();
                resultCustomer.ForEach(item =>
                {
                    listProduct.AddRange(item.Product.Select(x => new ProductHandbookProductRecord
                    {
                        Category = item.Category,
                        ProductId = x.ProductId,
                        ProductName = x.ProductName,
                        HasInfo = x.HasInfo
                    }));
                });
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    sColumns = pageRequest.ColumnNames,
                    aaData = listProduct.Select(x => new object[]{
                                                             x.Category,
                                                             x.ProductName,
                                                             x.HasInfo,
                                                             x.ProductId
                        }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region Action edit
        [Authorize]
        public ActionResult ProductInfo(string productId)
        {
            ProductInfoViewModel vm = new ProductInfoViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveProductHandbookProductDetailByProductIdRequest request = new RetrieveProductHandbookProductDetailByProductIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ProductInfo"
                    },
                    Lang = "vi",
                    ProductId = int.Parse(productId),
                };
                var customerRecords = new RetrieveProductHandbookProductDetailByProductIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveProductHandbookProductDetailByProductId(request);
                ProductHandbookProductDetailRecord record = (customerRecords == null || customerRecords.Record == null) ? new ProductHandbookProductDetailRecord() : customerRecords.Record;
                vm = new ProductInfoViewModel
                {
                    ProductId = int.Parse(productId),
                    ConditionProduct = record.Condition,
                    ConditionProductEn = record.ConditionEn,
                    OverviewProduct = record.Overview,
                    OverviewProductEn = record.OverviewEn,
                    ProductName = record.ProductName,
                    ProductNameEn = record.ProductNameEn,
                    XSellingProduct = record.Xselling,
                    XSellingProductEn = record.XsellingEn,
                    DocumentProduct = record.Document,
                    DocumentProductEn = record.DocumentEn
                };
                //vm.ProductID = productId;
            }
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ProductInfo(ProductInfoViewModel vm, FormCollection form)
        {
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                ResponseRecord response = null;
                UpdateProductHandbookProductDetailRequest request = new UpdateProductHandbookProductDetailRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.EditProductDetail"
                    },
                    Record = new ProductHandbookProductDetailRecord
                    {
                        ProductId = vm.ProductId,
                        ProductName = vm.ProductName,
                        Overview = vm.OverviewProduct,
                        Condition = vm.ConditionProduct,
                        Document = vm.DocumentProduct,
                        Xselling = vm.XSellingProduct,
                        ConditionEn = vm.ConditionProductEn,
                        DocumentEn = vm.DocumentProductEn,
                        OverviewEn = vm.OverviewProductEn,
                        ProductNameEn = vm.ProductNameEn,
                        XsellingEn = vm.XSellingProductEn
                    },
                    Lang = CurrentLang
                };
                response = serviceClient.Proxy.UpdateProductHandbookProductDetail(request);
                if (response.IsSuccess)
                {
                    return RedirectToAction("ProductInfo", "AdminProduct", new { productId = vm.ProductId });
                }
                else
                {
                    vm.Message = response.ErrorMessage;
                    vm.MessageType = MessageType.Error;
                    //vm.IsSupperAdmin = CurrentUser.Equals(SystemConfiguration.SUPPER_ADMIN, StringComparison.OrdinalIgnoreCase);
                }
            }
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult AddInfoProduct(string proId)
        {
            bool resultCustomer = false;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                AddProductInfoRequest request = new AddProductInfoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.RepreInfo"
                    },
                    CurrentDao = CurrentUserRecord,
                    Lang = CurrentLang,
                    Record = new ProductHandbookProductRecord
                    {
                        ProductId = int.Parse(proId)
                    }
                };
                var customerRecords = new ResponseRecord();
                customerRecords = serviceClient.Proxy.AddProductInfo(request);
                resultCustomer = customerRecords.IsSuccess;
            }
            return Json(resultCustomer);
        }
        #endregion
    }
}